//
//  ChatCell.swift
//  WebSocketDemo
//
//  Created by 姬向阳 on 2020/5/9.
//  Copyright © 2020 SUNNYJ. All rights reserved.
//

import SwiftUI

struct ChatCell: View {
    var message: Message
    
    var body: some View {
        Group {
            if message.isMe {
                VStack(alignment: .trailing) {
                    Text(message.time)
                        .font(.system(size: 11))
                        .foregroundColor(.secondary)
                    HStack(alignment: .top) {
                        Spacer()
                        Text(message.text)
                            .bold()
                            .padding(10)
                            .foregroundColor(.white)
                            .background(Color.green)
                            .cornerRadius(10)
                        Text(message.author)
                    }
                }
            }else {
                VStack(alignment: .leading) {
                    Text(message.time)
                        .font(.footnote)
                        .foregroundColor(.secondary)
                    HStack(alignment: .top) {
                        Text(message.author)
                        Text(message.text)
                            .bold()
                            .padding(10)
                            .foregroundColor(.white)
                            .background(Color.blue)
                            .cornerRadius(10)
                    }
                }
            }
        }
    }
}

struct ChatCell_Previews: PreviewProvider {
    static var previews: some View {
        ChatCell(message: Message(time: "2020-5-13 12:00:00", author: "SunnyJ", text: "Bye-bye Artifact, Hello World!", color: "green", isMe: true))
    }
}
